package com.opages.mvc.adapter.nacos.configuration;

import com.opages.mvc.adapter.common.properties.EnableConfigurationProperties;
import com.opages.mvc.adapter.common.client.serviceregistry.ServiceRegistry;
import com.opages.mvc.adapter.nacos.discovery.NacosDiscoveryClient;
import com.opages.mvc.adapter.nacos.model.NacosDiscoveryProperties;
import com.opages.mvc.adapter.nacos.serviceregistry.NacosAutoRegistration;
import com.opages.mvc.adapter.nacos.serviceregistry.NacosRegistration;
import com.opages.mvc.adapter.nacos.serviceregistry.NacosServiceRegistry;
import org.springframework.context.annotation.Bean;

import javax.servlet.ServletContext;

/**
 * @author daizhong.liu
 * @create 2020-07-18 9:05
 **/
@EnableConfigurationProperties(NacosDiscoveryProperties.class)
public class NacosAutoConfiguration {
    @Bean
    public NacosDiscoveryClient nacosDiscoveryClient(NacosDiscoveryProperties nacosDiscoveryProperties) {
        return new NacosDiscoveryClient(nacosDiscoveryProperties);
    }

    @Bean
    public NacosServiceRegistry nacosServiceRegistry(NacosDiscoveryProperties nacosDiscoveryProperties) {
        return new NacosServiceRegistry(nacosDiscoveryProperties);
    }

    @Bean
    public NacosRegistration nacosRegistration(NacosDiscoveryProperties nacosDiscoveryProperties, ServletContext servletContext) {
        return new NacosRegistration(nacosDiscoveryProperties,servletContext.getContextPath());
    }

    @Bean
    public NacosAutoRegistration nacosAutoRegistration(ServiceRegistry<NacosRegistration> serviceRegistry, NacosRegistration nacosRegistration) {
        return new NacosAutoRegistration(serviceRegistry,nacosRegistration);
    }
}
